/**
 * @description 父子关系的数组转换成树形结构数据
 * @param data
 * @returns {*}
 */
export function translateDataToTree(data) {
  const parent = data.filter(
    (value) =>
      value.parentId === "undefined" ||
      value.parentId == null ||
      value.parentId == "#" ||
      value.parentId == "0" ||
      value.parentId == ""
  );

  const children = data.filter(
    (value) =>
      (value.parentId !== "undefined" && value.parentId != null) ||
      value.parentId != "0" ||
      value.parentId != "#" ||
      value.parentId != ""
  );
  //   console.log("parent");
  //   console.log(parent);
  //   console.log(children);
  const translator = (parent, children) => {
    parent.forEach((parent) => {
      children.forEach((current, index) => {
        if (current.parentId === parent.id) {
          const temp = JSON.parse(JSON.stringify(children));
          temp.splice(index, 1);
          translator([current], temp);
          typeof parent.children !== "undefined"
            ? parent.children.push(current)
            : (parent.children = [current]);
        }
      });
    });
  };
  translator(parent, children);
  return parent;
}

/**
 * @description 树形结构数据转换成父子关系的数组
 * @param data
 * @returns {[]}
 */
export function translateTreeToData(data) {
  const result = [];
  data.forEach((item) => {
    const loop = (data) => {
      result.push({
        id: data.id,
        label: data.label,
        parentId: data.parentId,
        dataList: data.dataList,
        user: data.user,
        statusType: data.statusType,
      });
      const child = data.children;
      if (child) {
        for (let i = 0; i < child.length; i++) {
          loop(child[i]);
        }
      }
    };
    loop(item);
  });
  return result;
}

export function translateTreeToData1(data) {
  const result = [];
  data.forEach((item) => {
    const loop = (data) => {
      result.push({
        ...data,
      });
      const child = data.children;
      if (child) {
        for (let i = 0; i < child.length; i++) {
          loop(child[i]);
        }
      }
    };
    loop(item);
  });
  return result;
}
